package system;

import pannel.user.User;
import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public abstract class BasePage extends JPanel {
    protected JPanel headerPanel;
    protected JPanel contentArea;
    protected final User user;  // 替换role为完整的user对象

    public BasePage(User user) {
        this.user = user;
        setLayout(new BorderLayout());
        setBackground(Color.WHITE);
        initializeComponents();
        layoutComponents();
        initPageContent();  // 子类实现具体页面内容
    }

    private void initializeComponents() {
        // 初始化顶部标题栏，包含标题和用户信息
        headerPanel = new JPanel(new BorderLayout());
        headerPanel.setBackground(Color.WHITE);
        headerPanel.setPreferredSize(new Dimension(getWidth(), 60));
        headerPanel.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 20));

        // 右侧用户信息面板
        JPanel userInfoPanel = createUserInfoPanel();
        headerPanel.add(userInfoPanel, BorderLayout.EAST);

        // 初始化内容区域
        contentArea = new JPanel();
        contentArea.setBackground(Color.WHITE);
        contentArea.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
    }

    private JPanel createUserInfoPanel() {
        JPanel userInfoPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        userInfoPanel.setBackground(Color.WHITE);

        // 添加用户名和角色信息
        JLabel userLabel = new JLabel(user.getUsername() + " (" + user.getRole() + ")");
        userLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        userLabel.setForeground(new Color(102, 102, 102));

        // 添加当前时间
        JLabel timeLabel = new JLabel(getCurrentTime());
        timeLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        timeLabel.setForeground(new Color(153, 153, 153));

        userInfoPanel.add(timeLabel);
        userInfoPanel.add(Box.createHorizontalStrut(15));  // 间距
        userInfoPanel.add(userLabel);

        return userInfoPanel;
    }

    private String getCurrentTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }

    private void layoutComponents() {
        add(headerPanel, BorderLayout.NORTH);
        add(new JSeparator(), BorderLayout.CENTER);
        add(contentArea, BorderLayout.CENTER);
    }

    protected void setPageTitle(String title) {
        // 创建标题标签
        JLabel titleLabel = new JLabel(title);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
        titleLabel.setForeground(new Color(51, 51, 51));

        // 清除可能存在的旧标题并添加新标题
        // 注意：只替换WEST位置的组件，保留EAST位置的用户信息
        Component[] components = headerPanel.getComponents();
        for (Component comp : components) {
            if (headerPanel.getLayout() instanceof BorderLayout) {
                Object constraints = ((BorderLayout)headerPanel.getLayout()).getConstraints(comp);
                if (constraints == BorderLayout.WEST) {
                    headerPanel.remove(comp);
                }
            }
        }

        headerPanel.add(titleLabel, BorderLayout.WEST);
        headerPanel.revalidate();
        headerPanel.repaint();
    }

    // 获取当前用户信息的方法
    protected User getCurrentUser() {
        return user;
    }

    // 获取当前用户角色的便捷方法
    protected String getCurrentRole() {
        return user.getRole();
    }

    // 获取当前用户名的便捷方法
    protected String getCurrentUsername() {
        return user.getUsername();
    }

    // 子类需要实现的方法，用于初始化具体页面内容
    protected abstract void initPageContent();
}